package main;

import com.github.crab2died.ExcelUtils;
import com.github.crab2died.sheet.wrapper.NoTemplateSheetWrapper;
import model.statical.ADepartment;
import model.statical.BDepartment;
import model.statical.CDepartment;
import model.statical.DDepartment;
import model.statical.Model;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class StaticMain
{

	public static void main(String[] args)
	{
		List<Model> actualList, authorizedList;
		try
		{
			actualList = ExcelUtils.getInstance()
					.readExcel2Objects("数据模板/静态数据.xlsx", Model.class,1);
			authorizedList = ExcelUtils.getInstance()
					.readExcel2Objects("数据模板/静态数据.xlsx", Model.class,0);
		} catch (Exception e)
		{
			e.printStackTrace();
			return;
		}

		calculate(actualList, authorizedList);
	}
	
	public static void calculate(File file)
	{

		List<Model> actualList, authorizedList;
		
		try(InputStream inputStream = new FileInputStream(file))
		{
			actualList = ExcelUtils.getInstance().readExcel2Objects(inputStream,
					Model.class, 1);
		} catch (Exception e)
		{
			e.printStackTrace();
			return;
		}
		try(InputStream inputStream = new FileInputStream(file))
		{
			authorizedList = ExcelUtils.getInstance().readExcel2Objects(inputStream,
					Model.class, 0);
		} catch (Exception e)
		{
			e.printStackTrace();
			return;
		}

		calculate(actualList, authorizedList);
	}
	
	public static void calculate(List<Model> actualList,List<Model> authorizedList)
	{
		ArrayList<DDepartment> allDDepartments = new ArrayList<>();
		DDepartment tempDDepartment = null;
		for (Model model : authorizedList)
		{
			if (tempDDepartment == null
					|| !tempDDepartment.isModelInThisDDepartment(model))
			{
				tempDDepartment = new DDepartment(model);
				allDDepartments.add(tempDDepartment);
			}
			tempDDepartment.notMatchAuthorizedModels.add(model);
		}

		for (Model model : actualList)
		{
			for (DDepartment dDepartment : allDDepartments)
			{
				if (dDepartment.isModelInThisDDepartment(model))
				{
					dDepartment.notMatchActualModels.add(model);
					break;
				}
			}
		}

		System.out.println("D级单位总数量:" + allDDepartments.size());

		for (DDepartment dDepartment : allDDepartments)
			dDepartment.calculateStatisticData();
		
		List<CDepartment> allCDepartments=new ArrayList<>();
		CDepartment tempCDepartment=null;
		for (DDepartment dDepartment : allDDepartments)
		{
			if (tempCDepartment == null
					|| !tempCDepartment.isSubDepartmentInThis(dDepartment)) {
				tempCDepartment=new CDepartment(dDepartment);
				allCDepartments.add(tempCDepartment);
			}
			tempCDepartment.addSubDepartment(dDepartment);
		}
		
		System.out.println("C级单位总数量:" + allCDepartments.size());
		for (CDepartment cDepartment : allCDepartments)
			cDepartment.calculateStatisticData();
		
		List<BDepartment> allBDepartments=new ArrayList<>();
		BDepartment tempBDepartment=null;
		for (CDepartment cDepartment : allCDepartments)
		{
			if (tempBDepartment == null
					|| !tempBDepartment.isSubDepartmentInThis(cDepartment)) {
				tempBDepartment=new BDepartment(cDepartment);
				allBDepartments.add(tempBDepartment);
			}
			tempBDepartment.addSubDepartment(cDepartment);
		}
		
		System.out.println("B级单位总数量:" + allBDepartments.size());
		for (BDepartment bDepartment : allBDepartments)
			bDepartment.calculateStatisticData();
		
		
		List<ADepartment> allADepartments=new ArrayList<>();
		ADepartment tempADepartment=null;
		for (BDepartment bDepartment : allBDepartments)
		{
			if (tempADepartment == null
					|| !tempADepartment.isSubDepartmentInThis(bDepartment)) {
				tempADepartment=new ADepartment(bDepartment);
				allADepartments.add(tempADepartment);
			}
			tempADepartment.addSubDepartment(bDepartment);
		}
		
		System.out.println("A级单位总数量:" + allADepartments.size());
		for (ADepartment aDepartment : allADepartments)
			aDepartment.calculateStatisticData();
	
		
		List<NoTemplateSheetWrapper> sheets = new ArrayList<>();
		sheets.add(new NoTemplateSheetWrapper(allDDepartments, DDepartment.class, true, "D级单位统计数据"));
		sheets.add(new NoTemplateSheetWrapper(allCDepartments, CDepartment.class, true, "C级单位统计数据"));
		sheets.add(new NoTemplateSheetWrapper(allBDepartments, BDepartment.class, true, "B级单位统计数据"));
		sheets.add(new NoTemplateSheetWrapper(allADepartments, ADepartment.class, true, "A级单位统计数据"));
		
		try
		{
			ExcelUtils.getInstance().noTemplateSheet2Excel(sheets, "静态统计数据.xlsx");
			MainFrame.openFile(new File("静态统计数据.xlsx"));
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
